package com.songcha.module_home.ui.fragment.home

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookListBean
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.bean.configuration.SystemConfigurationResultBean
import com.songcha.library_business.config.VarConfig
import com.songcha.library_business.proxy.configuration.SystemConfigurationManager
import com.songcha.library_common.util.LogUtil
import com.songcha.library_network.bean.BaseListDataBean
import com.songcha.module_home.bean.HomeCateBean
import kotlin.random.Random

class HomeViewModel(app: Application):BaseViewModel<HomeRepository>(app) {
    val homeCateList=MutableLiveData<MutableList<HomeCateBean.DataBean>>()

    fun getHomeCate(){
        handleApiDataObserver(repository.getHomeCate(),object :IApiResult<HomeCateBean>{
            override fun onSuccess(bean: HomeCateBean) {
                onLoadSuccess()
                homeCateList.value=bean.data
            }

            override fun onError(e: Throwable) {

            }

        },true)

    }

    fun getHomeRecommendDialogBook(block:(book:BookNetBean)->Unit){
        handleApiDataObserver(repository.getHomeRecommendDialogBookList(),object :IApiResult<BaseListDataBean<BookNetBean>>{
            override fun onSuccess(bean: BaseListDataBean<BookNetBean>) {
                val size = bean.data.size
                if(size == 0) return
                val r = Random(System.currentTimeMillis()).nextInt(size)
                block(bean.data[r])
            }

            override fun onError(e: Throwable) {

            }

        })
    }

    fun getSystemConfiguration(){
        handleApiDataObserver(SystemConfigurationManager.getSystemConfiguration(),object:
            IApiResult<SystemConfigurationResultBean> {
            override fun onSuccess(bean: SystemConfigurationResultBean) {

            }

            override fun onError(e: Throwable) {

            }

        })
    }

}